package com.hospital.service.impl;

import com.hospital.dao.MedicineDao;
import com.hospital.entity.Medicine;
import com.hospital.service.MedicineService;
import com.hospital.utils.ArrayUtil;
import com.hospital.utils.Constants;
import com.hospital.vo.MedicineVO;
import com.hospital.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 药品的业务层实现类
 *
 * @author gzy
 * @date 2022/03/28
 */
@Slf4j
@DubboService
public class MedicineServiceImpl implements MedicineService {

    @Autowired
    private MedicineDao medicineDao;

    /**
     * 获取所有药品信息
     *
     * @return 药品信息列表
     */
    @Override
    public ResponseVO getAllMedicine(Integer pageNum, Integer pageSize) {
        log.info("获取所有药品信息");
        List<Medicine> list = medicineDao.getAllMedicine(pageNum, pageSize);

        list.forEach(System.out::println);

        if (list.size() > 0) {
            return ResponseVO.buildSuccessInstance(list);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
    }

    /**
     * 根据药品编号获取药品的详细信息
     *
     * @param medicineId 药品编号
     * @return 药品的详细信息
     */
    @Override
    public ResponseVO getMedicineDetails(String medicineId) {
        log.info("根据药品编号获取药品的详细信息");
        Medicine medicine = medicineDao.getMedicineDetails(medicineId);

        if (medicine != null) {
            return ResponseVO.buildSuccessInstance(medicine);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
    }

    /**
     * 添加药品信息
     *
     * @param medicineVO 药品的VO
     * @return 药品是否成功添加
     */
    @Override
    public ResponseVO addMedicine(MedicineVO medicineVO) {
        log.info("添加药品信息medicineVO = " + medicineVO);
        Medicine medicine = new Medicine();
        BeanUtils.copyProperties(medicineVO, medicine);

        Integer n = medicineDao.addMedicine(medicine);

        if (n > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.UPDATE_FAIL);
    }

    /**
     * 根据药品编号删除药品信息
     *
     * @param medicineIds 药品编号
     * @return 药品是否删除成功
     */
    @Override
    public ResponseVO delMedicine(String[] medicineIds) {

        log.info("删除药品信息medicineId = " + ArrayUtil.readData(medicineIds));

        /*
         * 删除操作返回的行数
         */
        Integer row = medicineDao.delMedicine(medicineIds);

        if (row > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS, row);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.DELETE_FAIL, 0);
    }

    /**
     * 根据药品编号修改药品信息
     *
     * @param medicineVO 药品的VO
     * @return 药品是否修改成功
     */
    @Override
    public ResponseVO updateMedicine(MedicineVO medicineVO) {
        log.info("修改药品信息");
        Medicine medicine = new Medicine();
        BeanUtils.copyProperties(medicineVO, medicine);

        Integer n = medicineDao.updateMedicine(medicine);

        if (n > 0) {
            return ResponseVO.buildSuccessInstance(ResponseVO.RESP_SUCCESS);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.UPDATE_FAIL);
    }

    /**
     * 条件搜索，模糊查询：根据药品名称和类别查询对应的药品集
     *
     * @param medicineName 药品名称
     * @param medicineType 药品类别
     * @return 药品的结果集
     */
    @Override
    public ResponseVO searchMedicine(String medicineName, Integer medicineType) {
        List<Medicine> list = medicineDao.searchMedicine(medicineName, medicineType);

        if (list.size() > 0) {
            return ResponseVO.buildSuccessInstance(list);
        }

        return ResponseVO.buildSuccessInstance(ResponseVO.NULL_RESULT);
    }
}
